package com.neu.controller;

import com.neu.pojo.Fund;
import com.neu.pojo.User;
import com.neu.service.FundService;
import com.neu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/fund")
public class FundController {
    @Autowired
    private FundService fundService;
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate<String,String> sessionRedisTemplate;

    @PostMapping("/getFundByPreference")
    public List<Fund> getFundByPreference(@RequestParam("session_key") String session_key,
                                          @RequestParam("length") int length){
        String openid=sessionRedisTemplate.opsForValue().get(session_key);
        User user = userService.getUserById(openid);
        List<Fund> list1 = null;

        if(user.getPreference()==0){
            list1 = fundService.getFundByTypeRandom("指数型",length*5);
            List<Fund> list2 = fundService.getFundByTypeRandom("股票型", length*5);
            list1.addAll(list2);
        }else if(user.getPreference()==1){
            list1 = fundService.getFundByTypeRandom("货币型",length);
            List<Fund> list2 = fundService.getFundByTypeRandom("债券型",length*2);
            List<Fund> list3 = fundService.getFundByTypeRandom("混合型",length*2);
            List<Fund> list4 = fundService.getFundByTypeRandom("QDII",length*2);
            list1.addAll(list2);
            list1.addAll(list3);
            list1.addAll(list4);
        }else{
            //TODO 用户偏好类型待丰富
        }
        List<Fund> result=new ArrayList<>();
        Random rand=new Random();
        for(int i=0;i<length;i++){
            result.add(list1.get(rand.nextInt(list1.size())));
        }
        return result;

    }
}
